/**
 * Created by guillaume on 16/01/14.
 */
function imageInitEdit($w) {
    if($w.width()<300)
        $('.page.active ul.gridster').data('gridster').resize_widget($w, 2, 1);
}

function imageInitSave($w) {
    return $w.find('#WidgetValue').val();
}

function imageInitView($w) {
}

$(document).ready(function() {

    $('.page.active').on('click', '#image_upload_button', function(e) {
        e.preventDefault();
        var form = $(this).closest('form');
        var options = {
            target:   '#image_upload_output',   // target element(s) to be updated with server response
            beforeSubmit:  beforeSubmit,  // pre-submit callback
            success: function afterSuccess(data){
                form.find('select#WidgetValue').append('<option value="'+data.image+'">'+data.image+'</option>')
                form.find('input#WidgetValue').val(data.image);
            },
            uploadProgress: OnProgress, //upload progress callback
            resetForm: true        // reset the form after successful submit,
        };

        options.url = form.find('#image_upload_link').val();
        form.ajaxSubmit(options);
        return false;
    });
});

function beforeSubmit(){
    //check whether client browser fully supports all File API
    if (window.File && window.FileReader && window.FileList && window.Blob)
    {
        var fsize = $('#image_upload_FileInput')[0].files[0].size; //get file size
        var ftype = $('#image_upload_FileInput')[0].files[0].type; // get file type
        //allow file types
        switch(ftype)
        {
            case 'image/png':
            case 'image/gif':
            case 'image/jpeg':
            case 'image/pjpeg':
            case 'text/plain':
            case 'text/html':
            case 'application/x-zip-compressed':
            case 'application/pdf':
            case 'application/msword':
            case 'application/vnd.ms-excel':
            case 'video/mp4':
                break;
            default:
                $("#image_upload_output").html("<b>"+ftype+"</b> Unsupported file type!");
                return false
        }

        //Allowed file size is less than 5 MB (1048576 = 1 mb)
        if(fsize>5242880)
        {
            alert("<b>"+fsize +"</b> Too big file! <br />File is too big, it should be less than 5 MB.");
            return false
        }
    }
    else
    {
        //Error for older unsupported browsers that doesn't support HTML5 File API
        alert("Please upgrade your browser, because your current browser lacks some new features we need!");
        return false
    }
}

function OnProgress(event, position, total, percentComplete)
{
    //Progress bar
    $('#image_upload_progressbox').show();
    $('#image_upload_progressbar').width(percentComplete + '%') //update progressbar percent complete
    $('#image_upload_statustxt').html(percentComplete + '%'); //update status text
    if(percentComplete>50)
    {
        $('#image_upload_statustxt').css('color','#000'); //change status text to white after 50%
    }
}